package com.example.demo.utils;

import lombok.Data;

/**
 * 自定义接口的响应信息
 */
@Data
public class ApiResponse<T> {

    private Integer code;

    private String msg;

    private T data;

    private ApiResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 响应成功
     * @return
     */
    public static <T> ApiResponse<T> success() {
        return success(null);
    }

    /**
     * 响应成功
     * @param data 响应数据
     * @return
     */
    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse<>(ApiResponseCodeEnum.SUCCESS.getCode(), ApiResponseCodeEnum.SUCCESS.getMsg(), data);
    }

    /**
     * 响应成功
     * @param data 响应数据
     * @param msg 响应消息
     * @return
     */
    public static <T> ApiResponse<T> success(T data, String msg) {
        return new ApiResponse<>(ApiResponseCodeEnum.SUCCESS.getCode(), msg, data);
    }

    /**
     * 响应失败
     * @param code 响应码
     * @param msg 响应的错误消息
     * @return
     */
    public static <T> ApiResponse<T> error(Integer code, String msg) {
        return new ApiResponse<>(code, msg, null);
    }

    /**
     * 响应失败
     * @param responseCodeEnum 响应枚举
     * @return
     */
    public static <T> ApiResponse<T> error(ApiResponseCodeEnum responseCodeEnum) {
        return new ApiResponse<>(responseCodeEnum.getCode(), responseCodeEnum.getMsg(),null);
    }

}
